package com.aatattoo.light_food_service.controller;

import com.aatattoo.light_food_service.config.error.ResultData;
import com.aatattoo.light_food_service.entity.User;
import com.aatattoo.light_food_service.entity.Verification;
import com.aatattoo.light_food_service.enumeration.ReturnCode;
import com.aatattoo.light_food_service.service.SmsService;
import com.aatattoo.light_food_service.service.TokenService;
import com.aatattoo.light_food_service.service.UserService;
import com.aatattoo.light_food_service.service.VerificationService;
import com.aatattoo.light_food_service.utils.TimeUtil;
import com.apistd.uni.UniException;
import com.apistd.uni.UniResponse;
import com.apistd.uni.sms.UniMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * @Author aatattoo
 * @Date 2021/12/14 9:31 上午
 * @msg:
 */
@Controller
@ResponseBody
@RequestMapping("/sms")
public class SendSMSController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private VerificationService verificationService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserService userService;
    @PostMapping("/register")
    public Object registerSMS(@RequestBody Verification verification){
        String code = smsService.getMsgCode();
        String nowTime = TimeUtil.systemTime();
        UniMessage uniMessage = smsService.sendSMS(verification.getPhoneNumber(), "669f38df",code);
        int tableExist = verificationService.isTableExist(verification.getPhoneNumber());
        if (tableExist==0){
            // 发送短信
            try {
                UniResponse res = uniMessage.send();
                if (res.message.equals("Success")){
                    verification.setCode(code);
                    verification.setCreateTime(nowTime);
                    verificationService.addVerification(verification);
                    return true;
                }else {
                    return false;
                }
            } catch (UniException e) {
                return false;
            }
        }else {
            Verification data = verificationService.findeByPhoneNumber(verification.getPhoneNumber());
            String oldTime=data.getCreateTime();
            try {
                long time = TimeUtil.getTimeDifference(oldTime, nowTime);
                if(time<1){
                    return ResultData.success(false, ReturnCode.RC406,"访问已拒绝,距离上次请求未超过1分钟");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            // 发送短信
            try {
                UniResponse res = uniMessage.send();
                if (res.message.equals("Success")){
                    data.setCode(code);
                    data.setCreateTime(nowTime);
                    verificationService.updataVerification(data);
                    return true;
                }else {
                    return false;
                }
            } catch (UniException e) {
                return false;
            }
        }

    }

    @PostMapping("/changePassword")
    public Object changePWDSMS(@RequestBody Verification verification){
        String code = smsService.getMsgCode();
        String nowTime = TimeUtil.systemTime();
        UniMessage uniMessage = smsService.sendSMS(verification.getPhoneNumber(), "2b23a928",code);
        Verification data = verificationService.findeByPhoneNumber(verification.getPhoneNumber());
        String oldTime=data.getCreateTime();
        try {
            long time = TimeUtil.getTimeDifference(oldTime, nowTime);
            if(time<1){
                return ResultData.success(false, ReturnCode.RC406,"访问已拒绝,距离上次请求未超过1分钟");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 发送短信
        try {
            UniResponse res = uniMessage.send();
            if (res.message.equals("Success")){
                data.setCode(code);
                data.setCreateTime(nowTime);
                verificationService.updataVerification(data);
                return true;
            }else {
                return false;
            }
        } catch (UniException e) {
            return false;
        }
    }


    @PostMapping("/forgetPassword")
    public Object forgetPWDSMS(@RequestBody Verification verification){
        String code = smsService.getMsgCode();
        String nowTime = TimeUtil.systemTime();
        UniMessage uniMessage = smsService.sendSMS(verification.getPhoneNumber(), "f45dd0a7",code);
        Verification data = verificationService.findeByPhoneNumber(verification.getPhoneNumber());
        String oldTime=data.getCreateTime();
        try {
            long time = TimeUtil.getTimeDifference(oldTime, nowTime);
            if(time<1){
                return ResultData.success(false, ReturnCode.RC406,"访问已拒绝,距离上次请求未超过1分钟");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 发送短信
        try {
            UniResponse res = uniMessage.send();
            if (res.message.equals("Success")){
                data.setCode(code);
                data.setCreateTime(nowTime);
                verificationService.updataVerification(data);
                return true;
            }else {
                return false;
            }
        } catch (UniException e) {
            return false;
        }
    }



    @PostMapping("/checkCode")
    public Object checkCode(@RequestBody Verification verification) {
        String nowTime = TimeUtil.systemTime();
        Verification data = verificationService.findeByPhoneNumber(verification.getPhoneNumber());
        String oldTime = data.getCreateTime();
        try {
            long time = TimeUtil.getTimeDifference(oldTime, nowTime);
            if (time >=15) {
                return ResultData.success(false, ReturnCode.RC403, "验证码已失效");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return verification.getCode().equals(data.getCode());
    }

    @PostMapping("/checkUser")
    public boolean checkUser(@RequestBody Verification verification,@RequestHeader("token")String token){
        int userId = tokenService.analysisUserId(token);
        User user = userService.fingById(userId);
        return user.getPhoneNumber().equals(verification.getPhoneNumber());
    }
}
